package memory;
public class twofenfa {
    public static int binarySearch(int[] arr, int target) {
        int low = 0;
        int high = arr.length - 1;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            int midValue = arr[mid];

            if (midValue == target) {
                return mid; // 找到目标值，返回索引
            } else if (midValue < target) {
                low = mid + 1; // 目标值在右侧，更新low
            } else {
                high = mid - 1; // 目标值在左侧，更新high
            }
        }
        return -1; // 如果循环结束仍未找到，则返回-1
    }
        public static void main(String[] args) {
            int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            int target = 7;
            int result = binarySearch(array, target);

            if (result != -1) {
                System.out.println("Element " + target + " is present at index " + result + ".");
            } else {
                System.out.println("Element " + target + " is not present in the array.");
            }
        }
}
